package com.xiaoyu.client.ui.activity.mall;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.mall.MallGoodsAdapter;
import com.xiaoyu.client.model.goods.GoodsBean;
import com.xiaoyu.client.model.goods.GoodsParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.fragment.main.MallFragment;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/12/7
 * @Description 商品各分类页面
 */
public class GoodsTagActivity extends BaseActivityWithTitle {
    @BindView(R.id.list_layout_no_data_img)
    RelativeLayout noDataImg;
    @BindView(R.id.grid_list_container)
    GridView mListContainer;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;

    private MallGoodsAdapter mGoodsAdapter;//产品适配器
    private List<GoodsParam> mGoodsList = new ArrayList<>();
    private int page = 1;
    private int totalPage;
    private String tagId;//类型id

    @Override
    protected void loadContentView(ViewGroup parent) {
        tagId = getIntent().getStringExtra(ClientConstants.GOODS_TAG_ID);
        String tagName = getIntent().getStringExtra(ClientConstants.GOODS_TAG_NAME);
        setTitleText(tagName);
        LayoutInflater.from(this).inflate(R.layout.activity_parts_limit, parent, true);
        ButterKnife.bind(this);
        initUI();
        getGoodsInfo();
    }

    private void initUI() {
        //商品列表
        mGoodsAdapter = new MallGoodsAdapter(this,mGoodsList);
        mListContainer.setAdapter(mGoodsAdapter);
        mListContainer.setOnItemClickListener(onItemClickListener);

        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });

        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setEnableLoadMore(true);
        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        mRefreshLayout.setNoMoreData(false);

    }


    //列表item点击监听
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(GoodsTagActivity.this,GoodsDetailsActivity.class);
            intent.putExtra(ClientConstants.GOODS_ID,mGoodsList.get(position).getGoodsid());
            startActivity(intent);
        }
    };

    //刷新
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mGoodsList.clear();
            mGoodsAdapter.notifyDataSetChanged();
            getGoodsInfo();
            mRefreshLayout.finishRefresh();
        }
    };


    //加载更多
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < totalPage) {
                page = page + 1;
                getGoodsInfo();
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(false);
            } else {
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
                ToastUtil.showBelowToast(GoodsTagActivity.this,"已经没有更多数据！！！");
            }
        }
    };


    /**
     * 获取商品信息
     */
    private void getGoodsInfo() {
        NetworkManager.getGoodsByTagList(page, tagId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseGoodsInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseGoodsInfoSuc(String result) {
        if (page == 1) {
            mGoodsList.clear();
            mGoodsAdapter.updateList(mGoodsList);
        }
        GoodsBean bean = new Gson().fromJson(result, GoodsBean.class);
        totalPage = Integer.parseInt(bean.getData().getCount());
        mGoodsList.addAll(bean.getData().getList());
        mGoodsAdapter.updateList(mGoodsList);
        if (totalPage ==0){
            noDataImg.setVisibility(View.VISIBLE);
        }
        if (totalPage == 1) {
            mRefreshLayout.setEnableLoadMore(false);
            if (mGoodsList.size() == 0){
                noDataImg.setVisibility(View.VISIBLE);
            }
        } else {
            mRefreshLayout.setEnableLoadMore(true);
        }
    }

}
